cmake_minimum_required(VERSION 3.8)
project(ros_web_bridge)

## Use C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

## By adding -Wall and -Werror, the compiler does not ignore warnings anymore,
## enforcing cleaner code.
add_definitions(-Wall -Werror)


#######################
## Find dependencies ##
#######################

find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()
# 使用PkgConfig查找Draco
find_package(PkgConfig REQUIRED)
pkg_check_modules(DRACO REQUIRED draco)

# 如果pkg-config找不到，尝试手动查找
if(NOT DRACO_FOUND)
  find_path(DRACO_INCLUDE_DIRS draco/compression/encode.h)
  find_library(DRACO_LIBRARIES draco)
  
  if(DRACO_INCLUDE_DIRS AND DRACO_LIBRARIES)
    set(DRACO_FOUND TRUE)
    message(STATUS "Found Draco manually: ${DRACO_LIBRARIES}")
  else()
    message(FATAL_ERROR "Could not find Draco library")
  endif()
endif()
###########
## Build ##
###########

ament_auto_add_library(${PROJECT_NAME} SHARED
  DIRECTORY src
)
# 添加Draco相关编译选项
target_include_directories(${PROJECT_NAME} 
  PUBLIC 
    ${DRACO_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME} 
  ${DRACO_LIBRARIES}
)

target_compile_options(${PROJECT_NAME} 
  PUBLIC 
    ${DRACO_CFLAGS_OTHER}
)
rclcpp_components_register_node(${PROJECT_NAME}
  PLUGIN ros_web_bridge::RosWebBridgeNode
  EXECUTABLE ${PROJECT_NAME}_node
)
find_package(PCL REQUIRED)
find_package(PkgConfig REQUIRED)

# 查找 Draco 库
find_path(DRACO_INCLUDE_DIR draco/compression/encode.h)
find_library(DRACO_LIBRARY draco)

include_directories(${PCL_INCLUDE_DIRS} ${DRACO_INCLUDE_DIR})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(draco_receiver src/draco_receiver.cpp)
target_link_libraries(draco_receiver ${PCL_LIBRARIES} ${DRACO_LIBRARY} ${PROJECT_NAME})
target_include_directories(draco_receiver PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

#############
## Testing ##
#############

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
  list(APPEND AMENT_LINT_AUTO_EXCLUDE
    ament_cmake_uncrustify
    ament_cmake_flake8
  )
  ament_lint_auto_find_test_dependencies()
endif()

#############
## Install ##
#############

ament_auto_package(
  INSTALL_TO_SHARE
  launch
)